home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#11 (Aug 86)
/
forth
/
exp⁄ln
next >
Wrap
Text File
|
1986-06-03
|
5KB
|
227 lines
( 32 bit floating point routines, 27.4.1986 J. Langowski )
only forth also assembler also sane
include" add.sub"
include" mul.sp"
include" div.sp"
CODE 4*
MOVE.L (A6)+,D0
ASL.L #2,D0
MOVE.L D0,-(A6)
RTS
END-CODE MACH
( extract biased exponent & mantissa from 32 bit FP # )
CODE get.exp
MOVE.L (A6)+,D0
MOVE.L D0,D1
SWAP.W D0
LSR.W #7,D0
ANDI.L #$FF,D0
MOVE.L D0,-(A6)
ANDI.L #$7FFFFF,D1
ORI.L #$3F800000,D1
MOVE.L D1,-(A6)
RTS
END-CODE
CODE stoi
MOVE.L (A6)+,D0
MOVE.L D0,D1
SWAP.W D0
LSR.W #7,D0
SUBI.B #127,D0
BMI @zero
BEQ @one
ANDI.L #$7FFFFF,D1
BSET #23,D1
CMP.B #8,D0
BCC @long.shift
LSL.L D0,D1
CLR.W D1
SWAP.W D1
LSR.L #7,D1
MOVE.L D1,-(A6)
RTS
@long.shift
LSL.L #7,D1
SUBQ.B #7,D0
CLR.L D2
@shifts LSL.L #1,D1
ROXL.L #1,D2
SUBQ.B #1,D0
BNE @shifts
CLR.W D1
SWAP.W D1
LSR.L #7,D1
LSL.L #8,D2
ADD.L D2,D2
OR.L D2,D1
MOVE.L D1,-(A6)
RTS
@zero CLR.L D0
MOVE.L D0,-(A6)
RTS
@one MOVEQ.L #1,D0
MOVE.L D0,-(A6)
RTS
END-CODE
: s>i dup 0< if stoi negate else stoi then ;
CODE itos
MOVE.L (A6)+,D0
BEQ @zero
CLR.L D1
MOVE.L #$7F,D2
@shifts CMPI.L #1,D0
BEQ @one
LSR.L #1,D0
ROXR.L #1,D1
ADDQ.L #1,D2
BRA @shifts
@one LSR.L #8,D1
LSR.L #1,D1
SWAP.W D2
LSL.L #7,D2
BCLR #31,D2
OR.L D2,D1
MOVE.L D1,-(A6)
RTS
@zero MOVE.L D0,-(A6)
RTS
END-CODE
hex
: i>s dup 0< if negate itos 80000000 or else itos then ;
decimal
: s. s>f f. ;
( vocabulary maths also maths definitions )
decimal
fp 9 float
-inf f>s constant -infinity
inf f>s constant infinity
1.0 f>s constant one
10. f>s constant ten
100. f>s constant hun
pi f>s constant pi.s
2.718281828 f>s constant eu
( exponential, natural log )
.9999964239 f>s constant a1ln
-.4998741238 f>s constant a2ln
.3317990258 f>s constant a3ln
-.2407338084 f>s constant a4ln
.1676540711 f>s constant a5ln
-.0953293897 f>s constant a6ln
.0360884937 f>s constant a7ln
-.0064535442 f>s constant a8ln
variable ln2table 1020 vallot
2.0 fln f>s constant ln2
: fill.ln2table
256 0 do ln2 i 127 - i>s s*
i 4* ln2table + !
loop
;
: ln.base
one s- a8ln over s*
a7ln s+ over s*
a6ln s+ over s*
a5ln s+ over s*
a4ln s+ over s*
a3ln s+ over s*
a2ln s+ over s*
a1ln s+ s*
;
: ln dup 0> if get.exp
ln.base
swap 4* ln2table + @
s+
else drop -infinity
then
;
: lnacc
1000 0 do
i . i i>s ln dup s.
i i>f fln fdup f.
s>f f- f. cr
loop
;
variable exptable 700 vallot
: fill.exptable
176 0 do i 87 - i>f fe^x f>s
i 4* exptable + !
loop
;
-.9999999995 f>s constant a1exp
.4999999206 f>s constant a2exp
-.1666653019 f>s constant a3exp
.0416573745 f>s constant a4exp
-.0083013598 f>s constant a5exp
.0013298820 f>s constant a6exp
-.0001413161 f>s constant a7exp
: exp.base a7exp over s*
a6exp s+ over s*
a5exp s+ over s*
a4exp s+ over s*
a3exp s+ over s*
a2exp s+ over s*
a1exp s+ s*
one s+
one swap s/
;
: exp dup s>i swap over i>s s- exp.base swap
dup -87 < if 2drop 0
else dup 88 > if 2drop infinity
else 87 + 4* exptable + @ ( get exp of integer part ) s* then
then
;
: expacc
1000 0 do
i . i i>s hun s/ exp dup s.
i i>f 100. f/ fe^x fdup f.
s>f f- f. cr
loop
;
: emptyloop 0 1000 0 do dup drop loop drop ;
: femptyloop 0. 1000 0 do fdup fdrop loop fdrop ;
: testexp ten one s+ 1000 0 do dup exp drop loop drop ;
: testfexp 11. 1000 0 do fdup fe^x fdrop loop fdrop ;
: testln ten one s+ 1000 0 do dup ln drop loop drop ;
: testfln 11. 1000 0 do fdup fln fdrop loop fdrop ;
: speed.test cr
." Testing 32 bit routines..." cr
." empty..." counter emptyloop timer cr
." exp..." counter testexp timer cr
." ln..." counter testln timer cr cr
." Testing SANE routines..." cr
." empty..." counter femptyloop timer cr
." exp..." counter testfexp timer cr
." ln..." counter testfln timer cr
;